/**
 * This example, like all Swing examples, exists in a package:
 * in this case, the "start" package.
 * If you are using an IDE, such as NetBeans, this should work 
 * seamlessly.  If you are compiling and running the examples
 * from the command-line, this may be confusing if you aren't
 * used to using named packages.  In most cases,
 * the quick and dirty solution is to delete or comment out
 * the "package" line from all the source files and the code
 * should work as expected.  For an explanation of how to
 * use the Swing examples as-is from the command line, see
 * http://docs.oracle.com/javase/javatutorials/tutorial/uiswing/start/compile.html#package
 */
package utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;
import javax.swing.*;
import env.Road;
import env.World;

public class ShowGraphGui {
	
	/**
	 * Create the GUI and show it. For thread safety, this method should be
	 * invoked from the event-dispatching thread.
	 * @param wtitle 
	 */
	private static void createAndShowGUI(String wtitle) {
		// Create and set up the window.
		JFrame frame = new JFrame(wtitle);
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		BufferedImage myPicture;
		try {
			myPicture = ImageIO.read(new File(wtitle+".png"));
			JLabel picLabel = new JLabel(new ImageIcon(myPicture));
			frame.getContentPane().add(picLabel);
		} catch (IOException e) {
			//  Auto-generated catch block
			e.printStackTrace();
		}

		// Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	private static void worldToGraph(World world, String title) {
		StringBuilder out = new StringBuilder();
		out.append("graph world {\nnode [shape = circle ];\nsize=\"7!\"\n");
		java.util.Iterator<Road> it = world.getRoads().iterator();
		while (it.hasNext()) {
			Road troad = it.next();
			out.append(troad.getE1().toString() + "--"
					+ troad.getE2().toString() + "[ label = "
					+ troad.toString() + "]\n");
			// System.out.print((troad.getE1().toString() + "--" +
			// troad.getE2().toString() + "[ label = " + troad.getCost() +
			// "]\n"));
		}
		out.append("}\n");
		PrintWriter writer;
		try {
			writer = new PrintWriter(title, "UTF-8");
			writer.print(out.toString());
			writer.close();
		} catch (FileNotFoundException | UnsupportedEncodingException e) {
			//  Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void showGraph(World world, final String title) {
		// Schedule a job for the event-dispatching thread:
		// creating and showing this application's GUI.
		// readGraph("graph.txt");
		
		worldToGraph(world,title);
		try {
			Process p = Runtime
					.getRuntime()
					.exec("Graphviz2.34\\bin\\dot.exe "+title+" -Tpng -O");
			p.waitFor();
		} catch (IOException | InterruptedException e) {
			// Auto-generated catch block
			e.printStackTrace();
		}
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			String wtitle = title;
			public void run() {
				createAndShowGUI(wtitle);
			}
		});
	}
}